<?php
header('Content-Type: application/json');

$dataFile = '../../data/heroes.json';

try {
    $input = json_decode(file_get_contents('php://input'), true);
    
    if (!isset($input['id']) || !isset($input['name']) || !isset($input['image']) || !isset($input['description'])) {
        throw new Exception('缺少必要参数');
    }
    
    $data = json_decode(file_get_contents($dataFile), true);
    
    // 更新英雄信息
    $updated = false;
    foreach ($data['heroes'] as &$hero) {
        if ($hero['id'] == $input['id']) {
            $hero['name'] = $input['name'];
            $hero['image'] = $input['image'];
            $hero['description'] = $input['description'];
            $updated = true;
            break;
        }
    }
    
    if (!$updated) {
        throw new Exception('未找到指定英雄');
    }
    
    file_put_contents($dataFile, json_encode($data, JSON_PRETTY_PRINT));
    
    echo json_encode(['success' => true]);
    
} catch (Exception $e) {
    http_response_code(500);
    echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}
?> 